<?php

defined('IN_HOUSE5') or exit('No permission resources.');
h5_base::load_sys_class('form','',0);
h5_base::load_sys_class('format','',0);
class index {
function __construct() {
$this->db = h5_base::load_model('search_model');
$this->content_db = h5_base::load_model('content_model');
}
public function init() {
$siteid = isset($_REQUEST['siteid']) &&trim($_REQUEST['siteid']) ?intval($_REQUEST['siteid']) : 1;
$SEO = seo($siteid);
$sitelist  = getcache('sitelist','commons');
$default_style = $sitelist[$siteid]['default_style'];
$default_city = $sitelist[$siteid]['default_city'];
$site_title = $sitelist[$siteid]['name'];
$copyright = $sitelist[$siteid]['copyright'];
$search_setting = getcache('search');
$setting = $search_setting[$siteid];
$search_model = getcache('search_model_'.$siteid);
$type_module = getcache('type_module_'.$siteid);
if(isset($_GET['q'])) {
if(trim($_GET['q'])=='') {
header('Location: '.APP_PATH.'index.php?s=search');exit;
}
$typeid = empty($_GET['typeid']) ?48 : intval($_GET['typeid']);
$time = empty($_GET['time']) ||!in_array($_GET['time'],array('all','day','week','month','year')) ?'all': trim($_GET['time']);
$page = isset($_GET['page']) ?intval($_GET['page']) : 1;
$pagesize = 10;
$q = safe_replace(trim($_GET['q']));
$q = htmlspecialchars(strip_tags($q));
$q = str_replace('%','',$q);
$search_q = $q;
if($time == 'day') {
$search_time = SYS_TIME -86400;
$sql_time = ' AND adddate > '.$search_time;
}elseif($time == 'week') {
$search_time = SYS_TIME -604800;
$sql_time = ' AND adddate > '.$search_time;
}elseif($time == 'month') {
$search_time = SYS_TIME -2592000;
$sql_time = ' AND adddate > '.$search_time;
}elseif($time == 'year') {
$search_time = SYS_TIME -31536000;
$sql_time = ' AND adddate > '.$search_time;
}else {
$search_time = 0;
$sql_time = '';
}
if($page==1 &&!$setting['sphinxenable']) {
$commend = $this->db->get_one("`typeid` = '$typeid' $sql_time AND `data` like '%$q%'");
}else {
$commend = '';
}
if($setting['sphinxenable']) {
$sphinx = h5_base::load_app_class('search_interface','',0);
$sphinx = new search_interface();
$offset = $pagesize*($page-1);
$res = $sphinx->search($q,array($siteid),array($typeid),array($search_time,SYS_TIME),$offset,$pagesize,'@weight desc');
$totalnums = $res['total'];
if(!empty($res['matches'])) {
$result = $res['matches'];
}
}else {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND `data` like '%$q%'";
$result = $this->db->listinfo($sql,'searchid DESC',$page,10);
}
if($setting['relationenble']) {
$this->keyword_db = h5_base::load_model('search_keyword_model');
if(strlen($q) <17 &&strlen($q) >5 &&!empty($segment_q)) {
$res = $this->keyword_db->get_one(array('keyword'=>$q));
if($res) {
$this->keyword_db->update(array('searchnums'=>'+=1'),array('keyword'=>$q));
}else {
h5_base::load_sys_func('iconv');
$pinyin = gbk_to_pinyin($q);
if(is_array($pinyin)) {
$pinyin = implode('',$pinyin);
}
$this->keyword_db->insert(array('keyword'=>$q,'searchnums'=>1,'data'=>$segment_q,'pinyin'=>$pinyin));
}
}
if(!empty($segment_q)) {
$relation_q = str_replace(' ','%',$segment_q);
}else {
$relation_q = $q;
}
$relation = $this->keyword_db->select("MATCH (`data`) AGAINST ('%$relation_q%' IN BOOLEAN MODE)",'*',10,'searchnums DESC');
}
if(!empty($result) ||!empty($commend['id'])) {
if($setting['sphinxenable']) {
foreach($result as $_v) {
$sids[] = $_v['attrs']['id'];
}
}else {
foreach($result as $_v) {
$sids[] = $_v['id'];
}
}
if(!empty($commend['id'])) {
$sids[] = $commend['id'];
}
$sids = array_unique($sids);
$where = to_sqls($sids,'','id');
$model_type_cache = getcache('type_model_'.$siteid,'search');
$model_type_cache = array_flip($model_type_cache);
$modelid = $model_type_cache[$typeid];
if($modelid) {
$this->content_db->set_model($modelid);
if(empty($this->content_db->model_tablename)) {
$this->content_db = h5_base::load_model('yp_content_model');
$this->content_db->set_model($modelid);
}
if($setting['sphinxenable']) {
$data = $this->content_db->listinfo($where,'id DESC',1,$pagesize);
$pages = pages($totalnums,$page,$pagesize);
}else {
$data = $this->content_db->select($where,'*');
$pages = $this->db->pages;
$totalnums = $this->db->number;
}
if(!empty($segment_q)) {
$replace = explode(' ',$segment_q);
foreach($replace as $replace_arr_v) {
$replace_arr[] =  '<font color=red>'.$replace_arr_v.'</font>';
}
foreach($data as $_k=>$_v) {
$data[$_k]['title'] = str_replace($replace,$replace_arr,$_v['title']);
$data[$_k]['description'] = str_replace($replace,$replace_arr,$_v['description']);
}
}else {
foreach($data as $_k=>$_v) {
$data[$_k]['title'] = str_replace($q,'<font color=red>'.$q.'</font>',$_v['title']);
$data[$_k]['description'] = str_replace($q,'<font color=red>'.$q.'</font>',$_v['description']);
}
}
}else {
$special_api = h5_base::load_app_class('search_api','special');
$data = $special_api->get_search_data($sids);
$totalnums = count($data);
}
}
$execute_time = execute_time();
$pages = isset($pages) ?$pages : '';
$totalnums = isset($totalnums) ?$totalnums : 0;
$data = isset($data) ?$data : '';
include	template('search','list');
}else {
include	template('search','index');
}
}
public function public_get_suggest_keyword() {
$url = $_GET['url'].'&q='.$_GET['q'];
$trust_url = array('c8430fcf851e85818b546addf5bc4dd3');
$urm_md5 = md5($url);
if (!in_array($urm_md5,$trust_url)) exit;
$res = @file_get_contents($url);
if(CHARSET != 'gbk') {
$res = iconv('gbk',CHARSET,$res);
}
echo $res;
}
public function public_suggest_search() {
h5_base::load_sys_func('iconv');
$pinyin = gbk_to_pinyin($q);
if(is_array($pinyin)) {
$pinyin = implode('',$pinyin);
}
$this->keyword_db = h5_base::load_model('search_keyword_model');
$suggest = $this->keyword_db->select("pinyin like '$pinyin%'",'*',10,'searchnums DESC');
foreach($suggest as $v) {
echo $v['keyword']."\n";
}
}
}
?>